<?php

    require_once Core_IO_Path::combine(SERUM_MODULE_PATH, 'Base', 'controllers', 'admin', 'IndexController.php');
    
    /**
     * @package      Modules
     * @subpackage   Admins_Controllers_Admin
	 *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Admins_Controllers_Admin_Preferences extends Base_Controllers_Admin_Index
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Public Methods                          //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Displays my account
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function edit()
        {
            $rep            = $this->_getResponse('json');
            $schema         = array();

            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            $preferences    = Doctrine_Query::create()
                                ->from('DbDisplayPreference dp')
                                ->where('dp.status = ?', DbBase::ENABLED)
                                ->execute();

            foreach ($preferences as $preference) {
                $schema[] = array(
                                    'id'            => $preference->id,
                                    'key'           => $preference->preference_key,
                                    'type'          => $preference->value_type,
                                    'value'         => $preference->getValue($admin)
                                 );
            }

            $rep->setResponseContent(array('schema' => $schema));
            return $rep;
        }

        /**
         * Save action
         *
         * @return      Core_Controller_Response_Abstract
         */
        public function save()
        {
            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->where('a.id = ?', Core_Authentification::getNamespace()->id)
                                ->fetchOne();

            foreach ($this->_getParams() as $param => $value) {
                $preference    = Doctrine_Query::create()
                                    ->from('DbDisplayPreference dp')
                                    ->where('dp.status = ?', DbBase::ENABLED)
                                    ->addWhere('dp.preference_key = ?', $param)
                                    ->fetchOne();

                if (!$preference) {
                    continue;
                }

                $preference->setValue($admin, $value);
            }

            $rep = $this->_getResponse('http');
            $rep->setTemplate('json.tpl');
            $rep->data->assign('json', Zend_Json::encode(array('success' => true, 'errors' => array())));
            return $rep;
        }
    }
